//
//  ShopHeaderView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/6/23.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "ShopHeaderView.h"
#import "MTShopSingleModel.h"
#import "FXBlurView.h"
#import <SDWebImage/SDWebImage.h>

@interface ShopHeaderView()

proStrong UIImageView *shopImgView;

proStrong UIView *infoView;

@end

@implementation ShopHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    //顶部视图
    self.backgroundColor = [UIColor whiteColor];
    self.userInteractionEnabled = YES;
    
    //背景封面
    UIImageView *faceImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    faceImgView.backgroundColor = [Utils colorHexString:@"#D9D9D9" alpha:1.0];
    [self addSubview:faceImgView];
    [faceImgView sd_setImageWithURL:[NSURL URLWithString:@"http://img2.imgtn.bdimg.com/it/u=3021078330,3633431892&fm=26&gp=0.jpg"] placeholderImage:nil];//店铺背景图片
    
    //模糊效果
    FXBlurView *bview = [[FXBlurView alloc] initWithFrame:faceImgView.bounds];
    bview.tintColor = [UIColor whiteColor];  //前景颜色
    bview.blurEnabled = YES;                //是否允许模糊，默认YES
    bview.blurRadius = 10.0;               //模糊半径
    bview.dynamic = YES;                   //动态改变模糊效果
    [faceImgView addSubview:bview];
    
    //渐变
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)[[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.5);
    gradientLayer.frame = CGRectMake(0, 0, self.width, self.height);
    [faceImgView.layer addSublayer:gradientLayer];
    
    //店铺图片
    self.shopImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, self.height - 78, 90, 90)];
    self.shopImgView.backgroundColor = [UIColor redColor];
    self.shopImgView.layer.cornerRadius = 4;
    //    shopImgView.layer.masksToBounds = YES;
    self.shopImgView.layer.shadowColor = kColorGray.CGColor;//shadowColor阴影颜色
    self.shopImgView.layer.shadowOffset = CGSizeMake(0,2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    self.shopImgView.layer.shadowOpacity = 0.4;//阴影透明度，默认0
    self.shopImgView.layer.shadowRadius = 3;//阴影半径，默认3
    [self addSubview:self.shopImgView];
    
    [self setShopInfoView];
}

//店铺信息视图
- (void)setShopInfoView {
    
    self.infoView = [[UIView alloc]init];
    self.infoView.frame = CGRectMake(self.shopImgView.right+10, self.shopImgView.y, self.width-(self.shopImgView.right+20), 78);
    [self addSubview:self.infoView];
    
    //店铺图标
    UIImageView *shopIcon = [[UIImageView alloc]init];
    shopIcon.image = [UIImage imageNamed:@"icon_brd_big"];
    [self.infoView addSubview:shopIcon];
    shopIcon.frame = CGRectMake(0, 2, shopIcon.image.size.width, 16);
    
    //店铺名称
    UILabel *shopNameLab = [Utils genLabelWithFrame:CGRectZero textAlign:NSTextAlignmentLeft fontSize:18 textColor:[UIColor whiteColor]];
    shopNameLab.frame = CGRectMake(shopIcon.right+5, 0, self.infoView.width-(shopIcon.right+15), 20);
    shopNameLab.text = @"棒棒店铺"; //店铺名称
    [self.infoView addSubview:shopNameLab];
    
    //公告
    UILabel *noticeLab = [Utils genLabelWithFrame:CGRectZero textAlign:NSTextAlignmentLeft fontSize:12 textColor:[UIColor whiteColor]];
    noticeLab.frame = CGRectMake(0, shopNameLab.bottom+8, self.infoView.width, 12);
    noticeLab.text = [NSString stringWithFormat:@"公告：%@",@"大家好呀呀呀呀"];
    [self.infoView addSubview:noticeLab];
    
    //认证
    UIView *cerView = [self setShopGradeAndCerViewWithType:@"1"];
    cerView.frame =CGRectMake(0, _infoView.height-28, cerView.width, 18);
    [_infoView addSubview:cerView];
    
    [self bringSubviewToFront:_shopImgView];
}

//店铺评分视图
- (UIView *)setShopGradeAndCerViewWithType:(NSString *)type {
    
    CGFloat max_X = 0;
    UIView *cerView = [[UIView alloc]init];
    //店铺评价
    //分数
    UILabel *gradeLab = [Utils genLabelWithFrame:CGRectZero textAlign:NSTextAlignmentLeft fontSize:18 textColor:[UIColor yellowColor]];
    NSString *grade = [NSString stringWithFormat:@"%@分",@"4.9"];
    NSMutableAttributedString *str = [Utils returnStringSize:grade rang:NSMakeRange(grade.length - 1,1) size:12];
    gradeLab.attributedText = str;
    gradeLab.adjustsFontSizeToFitWidth = YES;
    CGSize gradeSize = [Utils sizeWithFont:[UIFont systemFontOfSize:12] Str:grade withMaxWidth:100];
    gradeLab.frame = CGRectMake(max_X, 0, gradeSize.width+2, 18);
    [cerView addSubview:gradeLab];
    //超出预期
    UILabel *yuQiLab = [Utils genLabelWithFrame:CGRectZero textAlign:NSTextAlignmentLeft fontSize:12 textColor:[UIColor yellowColor]];
    yuQiLab.text = @"极好";
    CGSize yuQiSize = [Utils sizeWithFont:[UIFont systemFontOfSize:12] Str:yuQiLab.text withMaxWidth:100];
    yuQiLab.frame = CGRectMake(gradeLab.right+5, 4, yuQiSize.width+5, 14);
    
    [cerView addSubview:yuQiLab];

    //月售
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(yuQiLab.right+8, 1, 2, 16)];
    line.backgroundColor = [UIColor grayColor];
    [cerView addSubview:line];
    
    UILabel *sellCountLab = [Utils genLabelWithFrame:CGRectZero textAlign:NSTextAlignmentLeft fontSize:12 textColor:kColorLightGray];
    sellCountLab.text = @"月售1088";
    CGSize sellSize = [Utils sizeWithFont:[UIFont systemFontOfSize:12] Str:sellCountLab.text withMaxWidth:100];
    sellCountLab.frame = CGRectMake(line.right+8, 2, sellSize.width, 16);
    [cerView addSubview:sellCountLab];
    
    cerView.width = sellCountLab.right;
    
    return cerView;
}

/// 根据主滚动视图的Y偏移量刷新视图
/// @param offsetY Y偏移量
- (void)refreshWithOffsetY:(CGFloat)offsetY {
    
    if (offsetY <= 0) {
        
        //满减活动的透明度
        CGFloat rate = -offsetY/30;
        if (rate >= 1) {
            rate = 1;
        }
        if (rate <= 0) {
            rate = 0;
        }
        
        CGFloat infoViewRate = -offsetY/50;
        self.infoView.alpha = 1.0 *(1.0-infoViewRate);
        
        //店铺照片的平移动画
        CGFloat distance = self.width/2 - 55;
        CGFloat imgRate = -offsetY/65;
        if (imgRate >=1.0) {
            imgRate = 1.0;
        }
        self.shopImgView.x = imgRate*distance +10;
        
    } else {
        
        //店铺信息的透明度
        self.infoView.alpha = 1.0 ;
        //店铺照片的平移动画
        self.shopImgView.x = 10;
    }
}

/// 恢复原始状态
- (void)recovery {
    
    self.infoView.alpha = 1.0 ;
    self.shopImgView.x = 10;
}

/// 显示成最大变化状态
- (void)changeMax {
    
    self.infoView.alpha = 0 ;
    self.shopImgView.x = (self.width-self.shopImgView.width)/2;
}

@end
